<?php

namespace app\wap\model;

use app\admin\model\Category;
use app\admin\model\UserGoodsGroup;
use think\exception\ValidateException;
use think\Model;
use app\admin\validate\Goods as val_goods;
class Goods extends Model
{
    protected $autoWriteTimestamp = 'datetime';
    protected $json = ['album','server_id_json'];
    protected $jsonAssoc = true;



    public function getStatusAttr($value)
    {
        $Status = [0=>'下架',1=>'上架'];
        return $Status[$value];
    }


    public  function GetUserGoodsGroup(){

        return $this->hasOne(Category::class,'id','cate_id');
    }
    public  function GoodsList(){

        return self::select();
    }


    public function getCategory(){
        return  $this->hasOne(Category::class,'id','cate_id')->bind(['type','cate_name'=>'name']);
    }
    /***
     * 获取列表
     */

    public static function GetGoodsList(){

        $where=[];
        $keyword=input('keyword','','trim');
        $page = !empty(input('page')) ? input('page') : 1;//默认第一页
        $limit = !empty(input('limit')) ? input('limit') : 12;//前端不传默认每页15条
        input('sort') ?$order=input('sort'):$order=['id'=>'desc'];

        !empty($keyword) &&  $where[]=['name','like','%'.$keyword.'%'];
        input('status') && $where[]=['status','=',input('status')];
        (input('start_time') && input('end_time')) &&  $where[]=['create_time','between',[input('start_time'),input('end_time')]];
        $where['status']=1;
        $where['store_id']=SID;
        $result=self::where($where)->with(['getCategory'])
            ->visible(['getCategory' =>['type','name']])
            ->hidden(['album','getCategory'])->order($order)->page($page)->limit($limit)->select();


        return array(
            'count'=>count($result),
            'data'=>  $result
        );



        // return self::with(['GetUserInfo','GetUserGoodsGroup'])->visible(['GetUserInfo' => ['head_pic', 'username'],'GetUserGoodsGroup'=>['name']])->select();
    }



    public static function GoodsDetail(){

        return  self::where(['id'=>input('goods_id')])->find();
    }


}